package com.gw.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.gw.mapper.UtilMapper;
import com.gw.pojo.dto.AddUtilAppDTO;
import com.gw.pojo.dto.UtilPageQueryDTO;
import com.gw.pojo.entity.UtilApp;
import com.gw.result.PageResult;
import com.gw.service.UtilService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class UtilServiceImpl implements UtilService {

    @Autowired
    private UtilMapper utilMapper;

    /**
     * 添加工具软件
     * @param addUtilAppDTO
     * @return
     */
    public void add(AddUtilAppDTO addUtilAppDTO) {

        UtilApp utilApp = new UtilApp();
        BeanUtils.copyProperties(addUtilAppDTO, utilApp);

        utilMapper.add(utilApp);
    }

    /**
     * 分页查询工具软件
     * @param utilPageQueryDTO
     * @return
     */
    //TODO 暂定分页查询，后续跟前端商量，看看应该如何设计工具板块
    public PageResult pageQuery(UtilPageQueryDTO utilPageQueryDTO) {
        //select * from .. limit 0, 10
        PageHelper.startPage(utilPageQueryDTO.getPage(), utilPageQueryDTO.getPageSize());

        Page<UtilApp> page = utilMapper.pageQuery(utilPageQueryDTO);

        List<UtilApp> result = page.getResult();
        long total = result.size();

        return new PageResult(total, result);
    }

    /**
     * 删除工具类信息
     * @param id
     * @return
     */
    public void remove(int id) {
        utilMapper.remove(id);
    }
}
